//
//  QYReturnRecordViewController.m
//  QingYue
//
//  Created by Zheng on 2017/6/14.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYReturnRecordViewController.h"
#import "QYReturnRecordCell.h"
#import "QYReturnRecordModel.h"

@interface QYReturnRecordViewController ()

@property (nonatomic, strong) NSMutableArray<QYReturnRecordModel *> *dataArr;

@end

@implementation QYReturnRecordViewController

- (instancetype)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:style];
    if (self) {
        [self loadData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    self.title = @"归还记录";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self.tableView registerClass:[QYReturnRecordCell class] forCellReuseIdentifier:@"QYReturnRecordCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LoadData
- (void)loadData {
    
    [SVProgressHUD show];
    
    NSDictionary *param = @{@"username":[kUserDefaults objectForKey:@"username"]};
    
    [[YCRequestDataService sharedManager] Post:@"RecordPreReturn" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            
            [SVProgressHUD dismissWithDelay:.35];
            
            _dataArr = [NSMutableArray new];
            
            NSArray<NSDictionary *> *borrowViewList = response[@"borrowViewList"];
            
            [borrowViewList enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                QYReturnRecordModel *model = [[QYReturnRecordModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
            }];
            
            [self.tableView reloadData];
            
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

#pragma mark - TableviewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QYReturnRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QYReturnRecordCell" forIndexPath:indexPath];
    
    QYReturnRecordModel *model = _dataArr[indexPath.row];
    cell.model = model;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 146;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return .0001f;
}

@end
